'use strict';
require('select2');
require('select2/select2.css');
require('select2/select2-bootstrap.css');
//////////////////////////////////////////////////////////////////////////////
// select2
// =====================
// ##123789##flag
// =====================
// liuyp 2018年11月28日15: 30: 04
// liuyp 2019年1月4日10: 33: 10 优化更新
//////////////////////////////////////////////////////////////////////////////
// select2 配置
window.select2Config = function (data , search){
    for(var i = 0, l = data.length; i < l; i++){
        data[i].text = data[i].name;
    }
    return (search ? { data: data }: { data: data , minimumResultsForSearch: -1 });
}
function formatJson(json){
    return JSON.parse(
        JSON.stringify(json)
        .replace(/id\":/gi,"id\":")
        .replace(/name\":/gi,"text\":")
        .replace(/\"text\"\:null/gi,'"text":"null"')
   )
}
function select2($timeout, $compile) {
    if (!$.fn.select2) throw "Error : please inject select2 libs!";
    return {
        restrict: 'A',
        scope: {
            config: '=',
            ngModel: '='
        },
        link: function(scope, element, attrs) {
            var $element = $(element);
            var tagName = element[0].tagName,
            config = {
                allowClear: false,
                multiple: !!attrs.multiple,
                placeholder: attrs.placeholder || '请选择'
            };
            if (tagName === 'SELECT') {
                delete config.multiple;
                angular.extend(config, scope.config);
                $element.prepend('<option value=""></option>').val('').select2(config);
                scope.$watch('ngModel',
                function(newVal) {
                    setTimeout(function() {
                        $element.find('[value^="?"]').remove();
                        $element.select2('val', newVal);
                    },
                    0);
                },
                true);
                return false;
            }
            if (tagName === 'INPUT') {
                function changeDatas() {
                    angular.extend(config, scope.config || scope.dataset);
                    var data = config.data;
                    if (data) {
                        config.data = formatJson(data);
                    }
                    $element.select2('destroy').select2(config);
                    $element.select2('val', scope.ngModel);
                    setSelect2Model();
                }
                scope.$watch('dataset', changeDatas, true)
                scope.$watch('config', changeDatas, true)
                if (attrs.select2Model) {
                    $element.on('select2-selecting', setSelect2Model)
                }
                scope.$watch('ngModel',
                function(newVal) {
                    if (config.ajax || config.multiple) {
                        return false
                    }
                    $element.select2('val', newVal);
                    setSelect2Model();
                },
                true);
            };
            function setSelect2Model(e) {
                var value = e ? e.object: $element.select2('data');
                var t = element.scope();
                var a = (attrs.select2Model || '').replace(/^\.|\.$/g, '').split('.');
                for (var i = 0,
                l = a.length - 1; i < l; i++) {
                    if (a[i]) {
                        t = t[a[i]]
                    }
                }
                t[a[a.length - 1]] = value
            }
        }
    }
}
exports = module.exports = select2;
